<?php
/**
 * 后台菜单
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/8/29
 * Time: 下午6:34
 */

namespace app\control\controller;



use app\common\model\AdminMenuModel;
use think\facade\Config;
use think\facade\Request;

class MenuController extends AdminBaseController
{
    /**
     * 后台菜单
     */
    public function index(){
        $list=AdminMenuModel::getALL()['tree'];

        $this->assign("list",$list);
        return $this->fetch();
    }

    /**
     * 增加/编辑
     * @param int $id
     * @param int $pid
     * @return mixed
     */
    public function edit($id=0,$pid=0){
        if(Request::isPost()){
            $data=Request::param();

            $AM=new AdminMenuModel();
            $status=$AM->addMenu($data);

            if($status['status']){
                $this->success($status['msg']);
            }else{
                $this->error($status['msg']);
            }
        }else{
            $info=AdminMenuModel::get($id);
            $plist=AdminMenuModel::getALL()['tree'];

            $this->assign('control',$this->getControllers($info['app']));
            $this->assign("info",$info);
            $this->assign("module_list",Config::get('module_list'));
            $this->assign("plist",$plist);
            $this->assign("id",$id);
            $this->assign("pid",$pid);
            return $this->fetch();
        }
    }

    /**
     * 获取某个目录下的Controller.php文件名的函数
     * @return array
     */
    function getControllers($module) {
        if(!$module){
            return null;
        }

        $dir="../application/{$module}/controller";

        $pathList = glob($dir . '/*.php');
        $res = [];
        foreach($pathList as $key => $value) {
            $c=basename($value, 'Controller.php');
            if($c!='AdminBase' && $c!='Public') $res[] = $c;
        }
        return $res;
    }

    /**
     * 获取某个控制器的方法名的函数 （备用）
     * @param $className
     * @param string $base
     * @return array
     */
    function getActions() {
        $list1=get_class_methods('app\control\controller\AdController');
        $list=get_class_methods('app\control\controller\AdminBaseController');
        return array_diff($list1,$list);
    }

    /**
     * 状态修改
     */
    public function acStatus(){
        $id=Request::post("id");
        $status=Request::post("status");

        $am=AdminMenuModel::get($id);
        $am->status=$status;
        $st=$am->save();

        AdminMenuModel::getALL(1);
        if($st){
            $this->success("操作成功！");
        }else{
            $this->error("操作失败！");
        }
    }

    /**
     * 排序
     */
    public function acOrder(){
        $id=Request::post("id");
        $orders=Request::post("orders");

        $am=AdminMenuModel::get($id);
        $am->list_order=$orders;
        $st=$am->save();

        AdminMenuModel::getALL(1);
        if($st){
            $this->success("操作成功！");
        }else{
            $this->error("操作失败！");
        }
    }

    /**
     * 删除分类
     */
    public function del(){
        $id=Request::post("id");

        $AM=new AdminMenuModel();
        $status=$AM->del($id);

        if($status['status']){
            $this->success($status['msg']);
        }else{
            $this->error($status['msg']);
        }
    }

    /**
     * 批量删除
     */
    public function delAll(){
        $id=Request::post("ids");

        $AM=new AdminMenuModel();

        $error=0;
        $sussuc=0;
        foreach ($id as $vo){
            $status=$AM->del($vo);
            if(!$status['status']){
                $error++;
                continue;
            }else{
                $sussuc++;
            }
        }

        $this->success("成功删除{$sussuc}条，删除失败{$error}条");
    }

    /**
     * 更新菜单
     */
    public function menuUpdate(){
        AdminMenuModel::getMenu(1,$this->userInfo);

        $this->success("更新成功");
    }
}